import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:tobias/tobias.dart' ;

//支付宝支付
class PayZhiFuBao extends StatefulWidget {
  const PayZhiFuBao({
    super.key,
  });

  @override
  State<StatefulWidget> createState() {
    return _PayZhiFuBaoState();
  }
}

class _PayZhiFuBaoState extends State<PayZhiFuBao> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('支付宝支付'),
      ),
      body: Center(
        child: ElevatedButton(onPressed: () async {
          String mockOrderInfo = '''
app_id=2021000118631840&biz_content={"timeout_express":"30m","product_code":"QUICK_MSECURITY_PAY","total_amount":"0.01","subject":"测试商品","out_trade_no":"20250710000001"}&charset=utf-8&method=alipay.trade.app.pay&sign_type=RSA2&timestamp=2025-07-10 10:00:00&version=1.0&sign=MIIBIjANBgkqhki...==
''';
//
//           Tobias tobias = Tobias();
//           tobias.pay(mockOrderInfo);
          final tobias = Tobias();

          final bool hasAliPay = await tobias.isAliPayInstalled;
          if (!hasAliPay) {
            print("请先安装支付宝客户端");
            return;
          }

          try {
            // orderInfo 必须来自你服务器，已签名
            final  result = await tobias.pay(mockOrderInfo);
            print("支付状态码: ${result}");
            print("支付结果: ${result}");
          } catch (e) {
            print("支付发生错误: $e");
          }
        }, child: Text('aaaaa')),
      )
    );
  }

}